/**
 * @description: 
 * @version: 0.0.1
 * @author: ljr
 * @Date: 2023-09-13 14:07:35
 */

import { Provide } from '@midwayjs/core';
import { OrdersCostRecord } from './orders_cost.entity';
import { InjectRepository, InjectDataSource } from '@midwayjs/sequelize';
import { Repository, Sequelize } from 'sequelize-typescript';

@Provide()
export class OrdersCostRecordsModel {
  @InjectRepository(OrdersCostRecord)
  photoRepository: Repository<OrdersCostRecord>;

  @InjectDataSource()
  executeRawSQL: Sequelize;
  /**
   * 新增成本数据
   * @param {*} cost_name 
   * @param {*} cost_amount 
   * @param {*} cost_remark 
   * @param {*} order_id 
   * @returns 
   */
  async insertData(params: { cost_name: string, cost_amount: number, cost_remark: any, order_id: number }): Promise<any> {
    const sql = `INSERT INTO orders_cost 
    SET  cost_name = '${params.cost_name}',
      cost_amount = ${params.cost_amount},
      cost_remark = '${params.cost_remark}',
      order_id = ${params.order_id},
      created_at = now(),
      updated_at = now();`;
    const [results] = await this.executeRawSQL.query(sql);
    return results
  }

  /**
   * 更新成本数据
   * @param params 
   * @returns 
   */
  async updateDataById(params: { cost_name: string, cost_amount: number, cost_remark: any, id: number }): Promise<any> {
    const sql = `UPDATE orders_cost 
      SET cost_name = '${params.cost_name}',
          cost_amount = ${params.cost_amount},
          cost_remark = '${params.cost_remark}',
          updated_at = now()
      WHERE
          id = ${params.id}`;
    const [results] = await this.executeRawSQL.query(sql);
    return results
  }

  /**
   * 删除成本数据
   * @param params 
   * @returns 
   */
  async delDataById(id: number): Promise<any> {
    const sql = `delete from orders_cost where id = ${id}`;
    const [results] = await this.executeRawSQL.query(sql);
    return results
  }

  /**
   * 查询成本数据
   * @param params 
   * @returns 
   */
  async getDataByOrderId(orderId: number): Promise<any> {
    const sql = `select * from orders_cost where order_id = ${orderId}`;
    const [results] = await this.executeRawSQL.query(sql);
    return results
  }

}